package com.sunlands.qdcy.tongzhuo.service;

import com.google.common.collect.Lists;
import com.sunlands.qdcy.tongzhuo.constant.CommonResultMessage;
import com.sunlands.qdcy.tongzhuo.exception.CheckRunException;
import com.sunlands.qdcy.tongzhuo.feign.vo.UserInfoDTO;
import com.sunlands.qdcy.tongzhuo.model.DiaryDO;
import com.sunlands.qdcy.tongzhuo.model.UserDiaryReplyDO;
import com.sunlands.qdcy.tongzhuo.model.UserDiaryZanDO;
import com.sunlands.qdcy.tongzhuo.vo.MessageReplyVO;
import com.sunlands.qdcy.tongzhuo.vo.Page;
import com.sunlands.qdcy.tongzhuo.vo.UserDiaryZanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * 互动通知
 *
 * @author gaoyanfeng
 * @create 2019-06-26 15:16
 **/
@Service
@Slf4j
public class InteractionMessageService extends BaseService {

    @Autowired
    private UserDiaryZanService userDiaryZanService;
    @Autowired
    private DiaryService diaryService;
    @Autowired
    private UserDiaryReplyService userDiaryReplyService;

    public Page<UserDiaryZanVO> pageZanByZanedUserId(Long userId, Integer page, Integer size) {
        Specification<UserDiaryZanDO> specification = new Specification<UserDiaryZanDO>() {
            @Override
            public Predicate toPredicate(Root<UserDiaryZanDO> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = Lists.newArrayList();
                predicateList.add(criteriaBuilder.equal(root.get("zanedUserId").as(Long.class), userId));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Sort sort = new Sort(Sort.Direction.DESC, Lists.newArrayList("createTime"));
        PageRequest pageRequest = new PageRequest(page, size, sort).previous();
        org.springframework.data.domain.Page pageResult = userDiaryZanService.findAll(specification, pageRequest);
        List<UserDiaryZanDO> userDiaryZans = pageResult.getContent();
        if (null == userDiaryZans || userDiaryZans.size() == 0) {
            return null;
        }
        List<UserDiaryZanVO> userDiaryZanVOList = Lists.newArrayList();
        for (UserDiaryZanDO userDiaryZan : userDiaryZans) {
            UserDiaryZanVO diaryZan = UserDiaryZanVO.from(userDiaryZan);
            userDiaryZanVOList.add(diaryZan);
            UserInfoDTO userById = getUserById(userDiaryZan.getZanUserId());
            if (null == userById) {
                throw new CheckRunException(CommonResultMessage.USER_INVALID.message);
            }
            diaryZan.setLearnTargetName(userById.getCurrentLearnTargetName());
            diaryZan.setZanUserName(userById.getName());
            diaryZan.setImgUrls(userById.getAvatarUrl());
            if (UserDiaryZanDO.TypeEnums.DIARY == UserDiaryZanDO.TypeEnums.create(userDiaryZan.getType())) {
                DiaryDO byDiaryId = diaryService.findById(userDiaryZan.getTargetId());
                diaryZan.setZanedContent(byDiaryId.getContent());
            } else if (UserDiaryZanDO.TypeEnums.REPLY == UserDiaryZanDO.TypeEnums.create(userDiaryZan.getType())) {
                UserDiaryReplyDO byReplyId = userDiaryReplyService.getByReplyId(userDiaryZan.getTargetId());
                diaryZan.setZanedContent(byReplyId.getReplyContent());
                diaryZan.setRootId(byReplyId.getDiaryId());
            }
        }
        Page<UserDiaryZanVO> pageResultVO = new Page<>();
        pageResultVO.setRows(userDiaryZanVOList);
        pageResultVO.setTotal(pageResult.getTotalElements());
        return pageResultVO;
    }

    public Page<MessageReplyVO> messagePageByUserId(Long userId, Integer page, Integer size) {
        Specification<UserDiaryReplyDO> specification = new Specification<UserDiaryReplyDO>() {
            @Override
            public Predicate toPredicate(Root<UserDiaryReplyDO> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = Lists.newArrayList();
                predicateList.add(criteriaBuilder.equal(root.get("replyedUserId").as(Long.class), userId));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Sort sort = new Sort(Sort.Direction.DESC, Lists.newArrayList("createTime"));
        PageRequest pageRequest = new PageRequest(page, size, sort).previous();
        org.springframework.data.domain.Page pageResult = userDiaryReplyService.findAll(specification, pageRequest);
        List<UserDiaryReplyDO> content = pageResult.getContent();
        if (null == content || content.size() == 0) {
            return null;
        }
        List<MessageReplyVO> messageReplyVOS = Lists.newArrayList();
        for (UserDiaryReplyDO diaryReply : content) {
            MessageReplyVO messageReplyVO = MessageReplyVO.from(diaryReply);
            messageReplyVOS.add(messageReplyVO);
            UserInfoDTO userById = getUserById(messageReplyVO.getReplyUserId());
            if (userById == null) {
                throw new CheckRunException(CommonResultMessage.USER_INVALID.message);
            }
            messageReplyVO.setReplyUserAvatarUrl(userById.getAvatarUrl());
            messageReplyVO.setReplyUserName(userById.getName());
            messageReplyVO.setAuthor(userById.getId() == userId.longValue());
            if (UserDiaryReplyDO.TypeEnums.create(diaryReply.getType()) == UserDiaryReplyDO.TypeEnums.DIARY_COMMENT) {
                DiaryDO byDiaryId = diaryService.findById(diaryReply.getReplyedId());
                messageReplyVO.setReplyedContent(byDiaryId.getContent());
//                messageReplyVO.setImgUrls(byDiaryId.getImgUrls());
            } else {
                UserDiaryReplyDO byReplyId = userDiaryReplyService.getByReplyId(diaryReply.getReplyedId());
                messageReplyVO.setReplyedContent(byReplyId.getReplyContent());
            }
        }
        Page<MessageReplyVO> pageResultVO = new Page<>();
        pageResultVO.setRows(messageReplyVOS);
        pageResultVO.setTotal(pageResult.getTotalElements());
        return pageResultVO;
    }
}
